#include "myhead.h"

//进入2048
int Into2048()
{

	//初始化游戏
	game_init();
	
	//开始游戏
	game_start();
		
	return 0;
}

//进入2048
int Into2048_power()
{

	//初始化游戏
	game_init();
	
	//开始游戏
	game_start_power();
		
	return 0;
}

//初始化游戏
void game_init()
{
	score = 0;
	bzero(game_map , sizeof(game_map) );
	
	for( int i = 0 ; i<800*480 ; i++)
	{
		*(map+i) = 0xffffff;
	}
	//显示游戏界面
	ShowJpg(map,"Game_Pic/2048_UI.jpg",0,0,1,1);
	
	//显示分数
	display_score( score);
	
	//生成一个初始数
	new_node();
}

//显示分数
void display_score( int new_score)
{
	char  Path_Name [1024] ; 
	
	int Unit 		=	new_score/1%10;
	int Ten 		=	new_score/10%10;
	int Hundreds 	=	new_score/100%10;
	int Thousands 	= 	new_score/1000%10;
	int Myriabit 	=	new_score/10000%10;
	
	bzero(Path_Name , 1024 );
	sprintf(Path_Name , "Game_Pic/%d.jpg" ,Myriabit );
	ShowJpg(map,Path_Name,55,180,1,1);
	
	bzero(Path_Name , 1024 );
	sprintf(Path_Name , "Game_Pic/%d.jpg" ,Thousands );
	ShowJpg(map,Path_Name,105,180,1,1);

	bzero(Path_Name , 1024 );
	sprintf(Path_Name , "Game_Pic/%d.jpg" ,Hundreds );
	ShowJpg(map,Path_Name,155,180,1,1);
	
	bzero(Path_Name , 1024 );
	sprintf(Path_Name , "Game_Pic/%d.jpg" ,Ten );
	ShowJpg(map,Path_Name,205,180,1,1);

	bzero(Path_Name , 1024 );
	sprintf(Path_Name , "Game_Pic/%d.jpg" ,Unit );
	ShowJpg(map,Path_Name,255,180,1,1);
}

//产生一个新的随机数 
int  new_node()
{
	int tmp = 0 , tmp_x = 0 , tmp_y = 0   ;
	int new_num = 0 ;
	
	//检查地图是否有空位产生新数字 
	bool flag = is_full();
	if (flag)
	{
		return -1;
	}
	
	//产生随机数
	srand(time (0));
	tmp = (int) (10.0 * rand() / (RAND_MAX + 1.0));
	printf("tmp = %d \n" , tmp );
	if (tmp < 5)
	{
		new_num = 2 ;
	}
	else
	{
		new_num = 4 ;
	}
	
	//产生随机位置
	while(1)
	{
		tmp_x = (int) (4.0 * rand() / (RAND_MAX + 1.0));
		
		tmp_y = (int) (4.0 * rand() / (RAND_MAX + 1.0));
		//找到空位置，将数字填进去
		if( game_map[tmp_x][tmp_y] == 0 )
		{
			printf("%d   %d\n" ,tmp_x,tmp_y );
			game_map[tmp_x][tmp_y] = new_num ; 
			break;
		}
	}
	
	//刷新显示
	show_map();
	
}

//产生一个新的随机数（升级版）
int  new_node_power()
{
	int tmp = 0 , tmp_x = 0 , tmp_y = 0   ;
	int new_num = 0 ;
	
	//检查地图是否有空位产生新数字 
	bool flag = is_full();
	if (flag)
	{
		return -1;
	}
	
	//产生随机数
	srand(time (0));
	tmp = (int) (10.0 * rand() / (RAND_MAX + 1.0));
	printf("tmp = %d \n" , tmp );
	if (tmp < 5)
	{
		new_num = 512 ;
	}
	else
	{
		new_num = 512 ;
	}
	
	//产生随机位置
	while(1)
	{
		tmp_x = (int) (4.0 * rand() / (RAND_MAX + 1.0));
		
		tmp_y = (int) (4.0 * rand() / (RAND_MAX + 1.0));
		//找到空位置，将数字填进去
		if( game_map[tmp_x][tmp_y] == 0 )
		{
			printf("%d   %d\n" ,tmp_x,tmp_y );
			game_map[tmp_x][tmp_y] = new_num ; 
			break;
		}
	}
	
	//刷新显示
	show_map();
	
}

//检查是否有空位
bool is_full( )
{
	int x, y , seat = 0;
	for (y = 0; y < 4; y++) 
	{
		for (x = 0; x < 4; x++)
		{
			if ( game_map[x][y] == 0 )
			{
				seat ++ ;
			}

		}
	}
	if (0 == seat )
	{
		printf("FULL !!! \n");
		return true ;
	}
	else
	{
		printf("Seat : %d\n", seat );
		return false;
	}
}

//刷新方块数字显示
int show_map()
{
	char * FileName = calloc(1,1024);
	int x , y ;
	for (y = 0; y < 4; y++) 
	{
		for (x = 0; x < 4; x++)
		{
			//只要不为零，就刷个数出来 ，否则刷空白
			if ( game_map[x][y] != 0 )
			{
				sprintf(FileName ,"Game_Pic/num_%d.jpg" , game_map[x][y] );

                ShowJpg(map , FileName , 380+(x*91) , 38+(y*91) , 1 , 1);
			}
			else
			{
                ShowJpg(map , "Game_Pic/while.jpg" , 380+(x*90) , 38+(y*90) , 1 , 1);
			}
		}
	}
	
	printf("刷新完成！\n");
	return 0 ;
}

//开始游戏
void game_start()
{
	int x , y; 
	int map_full = 0;
	
	int ret ; 
    //刷新作用
	Get_side();
    huigui:
	while(1)
	{	
		//获取坐标
        Get_side();
        ret = Analyze_Coordinates();
		//退出2048
		if(x_s >= 0 && x_e<= 100 && y_s >= 0 && y_e <= 60 )
		{
			return ; 
		}
        //判断是否点击了重来
        else if(x_s >= 205 && x_e<= 275 && y_s >= 310 && y_e <= 370 )
        {
            //重新初始化游戏
            game_init(map);
            map_full = 0 ;
        }
        //判断是否点击了 “开发者选项”
        else if(x_s >= 25 && x_e<= 85 && y_s >= 310 && y_e <= 370 )
        {
            printf("欢迎进入开发者选项\n");
            int pass_num = Password_Interface();
            if(pass_num == 1)
            {
                //提示变强
                ShowJpg(map,"Game_Pic/power.jpg",0,0,1,1);

                //获取坐标
                Get_side();
                if(Analyze_Coordinates() == 4)
                {
                    Into2048_power(map);
                }
            }
            return ;
        }

        //判断游戏是否通关成功
        for (y = 0; y < 4; y++) 
        {
            for (x = 0; x < 4; x++)
            {
                //只要不为零，就刷个数出来 ，否则刷空白
                if ( game_map[x][y] == 2048 )
                {
                    //通关成功
                    ShowJpg(map,"Game_Pic/2048_Sucess.jpg",0,0,1,1);
                    while (1)
                    {
                        Get_side();
                        if( x_s >= 33 && x_e <= 125 && y_s >= 365 && y_e <= 465)
                        {
                            //重新初始化游戏
                            game_init(map);
                            map_full = 0 ;
                        }
                        else if( x_s >= 220 && x_e <= 315 && y_s >= 365 && y_e <= 465 )
                        {
                            //返回
                            return ;
                        }
                    }
                }
            }
        }
		
        //判断是否可以继续游戏
		if( !map_full)
		{
			
			if( ret==3 )
			{
				do_right();
				map_full = new_node(map);
			}
			else if( ret==0)
			{
				do_up();
				map_full = new_node(map);
			}
			else if( ret == 2)
			{
				do_left();
				map_full = new_node(map);
			}
			else if( ret == 1)
			{
				do_down();
				map_full = new_node(map);
			}
		
			display_score(score);
			
		}
		else 
		{   
            //输了
            ShowJpg(map,"Game_Pic/2048_Game_Over.jpg",0,0,1,1);
            while (1)
            {
                Get_side();
                if( x_s >= 50 && x_e <= 130 && y_s >= 370 && y_e <= 470)
                {
                    //重新初始化游戏
                    game_init(map);
                    map_full = 0 ;
                    goto huigui;
                }
                else if( x_s >= 235 && x_e <= 315 && y_s >= 365 && y_e <= 465 )
                {
                    //返回
                    break;
                }
            }
            break;
		}
	}
	
	return ; 
}

//开始游戏(升级版)
void game_start_power()
{
	int x , y; 
	int map_full = 0;
	
	int ret ; 
    //刷新作用
	Get_side();
    huigui:
	while(1)
	{	
		//获取坐标
        Get_side();
        ret = Analyze_Coordinates();
		//退出2048
		if(x_s >= 0 && x_e<= 100 && y_s >= 0 && y_e <= 60 )
		{
			return ; 
		}
        //判断是否点击了重来
        else if(x_s >= 205 && x_e<= 275 && y_s >= 310 && y_e <= 370 )
        {
            //重新初始化游戏
            game_init(map);
            map_full = 0 ;
        }
        //判断是否点击了 “开发者选项”
        else if(x_s >= 25 && x_e<= 85 && y_s >= 310 && y_e <= 370 )
        {
            printf("欢迎进入开发者选项\n");
            int pass_num = Password_Interface(map);
            if(pass_num == 1)
            {
                //提示变强
                ShowJpg(map,"Game_Pic/power.jpg",0,0,1,1);

                //获取坐标
                Get_side();
                if(Analyze_Coordinates() == 4)
                {
                    Into2048_power(map);
                }
            }

            return ;

            
        }

        //判断游戏是否通关成功
        for (y = 0; y < 4; y++) 
        {
            for (x = 0; x < 4; x++)
            {
                //只要不为零，就刷个数出来 ，否则刷空白
                if ( game_map[x][y] == 2048 )
                {
                    //通关成功
                    ShowJpg(map,"Game_Pic/2048_Sucess.jpg",0,0,1,1);
                    while (1)
                    {
                        Get_side();
                        if( x_s >= 33 && x_e <= 125 && y_s >= 365 && y_e <= 465)
                        {
                            //重新进入游戏
                            Into2048(map);
                            return ;
                        }
                        else if( x_s >= 220 && x_e <= 315 && y_s >= 365 && y_e <= 465 )
                        {
                            //返回
                            return ;
                        }
                    }
                }
            }
        }
		
        //判断是否可以继续游戏
		if( !map_full)
		{
			
			if( ret==3 )
			{
				do_right();
				map_full = new_node_power(map);
			}
			else if( ret==0)
			{
				do_up();
				map_full = new_node_power(map);
			}
			else if( ret == 2)
			{
				do_left();
				map_full = new_node_power(map);
			}
			else if( ret == 1)
			{
				do_down();
				map_full = new_node_power(map);
			}
		
			display_score(score);
			
		}
		else 
		{   
            //输了
            ShowJpg(map,"Game_Pic/2048_Game_Over.jpg",0,0,1,1);
            while (1)
            {
                Get_side();
                if( x_s >= 50 && x_e <= 130 && y_s >= 370 && y_e <= 470)
                {
                    //重新初始化游戏
                    game_init(map);
                    map_full = 0 ;
                    goto huigui;
                }
                else if( x_s >= 235 && x_e <= 315 && y_s >= 365 && y_e <= 465 )
                {
                    //返回
                    break;
                }
            }
            break;
		}
	}
	
	return ; 
}

//右移
int do_right ()
{
	int x ,y , tmp ,tmp1 , tmp2 , num = 0 ;
    
    int i =4;
	for( i =4 ; i>0 ;i--)
	{	
		for (y = 0; y < 4; y++)
		{
			for (x = 0; x < 3; x++)
			{
				tmp = x ;
				while (game_map[tmp+1][y] != 0 && game_map[x][y]==game_map[tmp+1][y])
				{
					game_map[tmp+1][y] = game_map[x][y] + game_map[tmp+1][y];
					game_map[x][y] = 0 ;
					score += game_map[tmp+1][y];
				}
			}
		}


	
		for (y = 0; y < 4; y++)
		{
			for (x = 0; x < 4; x++)
			{
				tmp = x ;
				while (game_map[tmp+1][y] == 0 && game_map[tmp][y] != 0 && tmp <= 2)
				{
					game_map[tmp+1][y] = game_map[tmp][y] ;
					game_map[tmp][y] = 0 ;
					tmp ++;
				}
			}
		}

	}
		
	
	printf("右移完成！\n");

	return 0 ;
	
	
	
}

//左移
int do_left ()
{
	int x ,y , tmp ,tmp1 , tmp2 , num = 0 ;

    int i =4;

	for( i =4 ; i>0 ;i--)
	{
		for (y = 0; y < 4; y++)
		{
			for (x = 1; x < 4; x++)
			{
				tmp = x ;
				while (game_map[tmp-1][y] != 0 && game_map[x][y]==game_map[tmp-1][y])
				{
					game_map[tmp-1][y] = game_map[x][y] + game_map[tmp-1][y];
					game_map[x][y] = 0 ;
					score += game_map[tmp-1][y];
				}
			}
		}

	
	
		for (y = 0; y < 4; y++)
		{
			for (x = 1; x < 4; x++)
			{
				tmp = x ;
				while (game_map[tmp-1][y] == 0 && game_map[tmp][y] != 0 && tmp >= 1)
				{
					game_map[tmp-1][y] = game_map[tmp][y] ;
					game_map[tmp][y] = 0 ;
					tmp --;
				}
			}
		}

	
	}
		
	printf("左移完成！\n");

	return 0 ;
}

//上移
int do_up ()
{
	int x ,y , tmp ,tmp1 , tmp2 , num = 0 ;

    int i =4;

	for( i = 4 ; i>0 ;i-- )
	{
	
		for (x = 0; x < 4; x++)
		{
			for (y = 0; y < 3; y++)
			{
				tmp = y ;
				while (game_map[x][y+1] != 0 && game_map[x][y]==game_map[x][y+1])
				{
					game_map[x][y+1] = game_map[x][y] + game_map[x][y+1];
					game_map[x][y] = 0 ;
					score += game_map[x][y+1];
				}
			}
		}
		
			for (x = 0; x < 4; x++)
			{
				for (y = 3; y > 0; y--)
				{
					tmp = y ;
					while (game_map[x][tmp-1] == 0 && game_map[x][tmp] != 0 && tmp >= 1)
					{
						game_map[x][tmp-1] = game_map[x][tmp] ;
						game_map[x][tmp] = 0 ;
						tmp --;
					}
				}
			}

	}
	
	
	printf("上移完成！\n");

	return 0 ;
}

//下移
int do_down ()
{
	int x ,y , tmp ,tmp1 , tmp2 , num = 0 ;

    int i =4;

	for( i = 4 ; i>0 ;i-- )
	{
		for (x = 0; x < 4; x++)
		{
			for (y = 0; y < 3; y++)
			{
				tmp = y ;
				while (game_map[x][tmp+1] != 0 && game_map[x][y]==game_map[x][tmp+1])
				{
					game_map[x][tmp+1] = game_map[x][y] + game_map[x][tmp+1];
					game_map[x][y] = 0 ;
					score += game_map[x][tmp+1];
				}
			}
		}
		
	
			for (x = 0; x < 4; x++)
			{
				for (y = 0; y < 4; y++)
				{
					tmp = y ;
					while (game_map[x][tmp+1] == 0 && game_map[x][tmp] != 0 && tmp <= 2)
					{
						game_map[x][tmp+1] = game_map[x][tmp] ;
						game_map[x][tmp] = 0 ;
						tmp ++;
					}
				}
			}

	}
		
	
	printf("下移完成！\n");

	return 0 ;
}

//进入密码界面
int Password_Interface()
{
    char buf[6] = {0};
    int count = 0;

    //清空数组
    bzero(buf , 6);
    while (1)
    {
        //显示密码界面
        ShowJpg(map,"Game_Pic/password_UI.jpg",0,0,1,1);

        while (1)
        {
            //获取坐标
            Get_side();

            if(x_s>240 && x_e<340)
            {
                if(y_s>170 && y_e<245)//点击1
                {
                    buf[count] = '1';
                    ShowJpg(map,"Game_Pic/1.jpg",(313+count*50),84,1,1);
                    count++;
                }
                else if(y_s>245 && y_e<320)//点击4
                {
                    buf[count] = '4';
                    ShowJpg(map,"Game_Pic/4.jpg",(313+count*50),84,1,1);
                    count++;
                }
                else if(y_s>320 && y_e<395)//点击7
                {
                    buf[count] = '7';
                    ShowJpg(map,"Game_Pic/7.jpg",(313+count*50),84,1,1);
                    count++;
                }
                else if(y_s>395 && y_e<470)//点击退格
                {
                    ShowJpg(map,"Game_Pic/password_num_white.jpg",(313+(count-1)*50),84,1,1);
                    count--;
                }
            }
            else if(x_s>340 && x_e<440)
            {
                if(y_s>170 && y_e<245)//点击2
                {
                    buf[count] = '2';
                    ShowJpg(map,"Game_Pic/2.jpg",(313+count*50),84,1,1);
                    count++;
                }
                else if(y_s>245 && y_e<320)//点击5
                {
                    buf[count] = '5';
                    ShowJpg(map,"Game_Pic/5.jpg",(313+count*50),84,1,1);
                    count++;
                }
                else if(y_s>320 && y_e<395)//点击8
                {
                    buf[count] = '8';
                    ShowJpg(map,"Game_Pic/8.jpg",(313+count*50),84,1,1);
                    count++;
                }
                else if(y_s>395 && y_e<470)//点击0
                {
                    buf[count] = '0';
                    ShowJpg(map,"Game_Pic/0.jpg",(313+count*50),84,1,1);
                    count++;
                }
            }
            else if(x_s>440 && x_e<540)
            {
                if(y_s>170 && y_e<245)//点击3
                {
                    buf[count] = '3';
                    ShowJpg(map,"Game_Pic/3.jpg",(313+count*50),84,1,1);
                    count++;
                }
                else if(y_s>245 && y_e<320)//点击6
                {
                    buf[count] = '6';
                    ShowJpg(map,"Game_Pic/6.jpg",(313+count*50),84,1,1);
                    count++;
                }
                else if(y_s>320 && y_e<395)//点击9
                {
                    buf[count] = '9';
                    ShowJpg(map,"Game_Pic/9.jpg",(313+count*50),84,1,1);
                    count++;
                }
                else if(y_s>395 && y_e<470)//点击清空
                {
                    bzero(buf , 6);//清空数组
                    count = 0;
                    ShowJpg(map,"Game_Pic/password_white.jpg",313,84,1,1);
                }
            }
            else if(x_s>0 && y_s>0 && x_e<100 && y_e<80)
            {
                //返回
                return 0;
            }

            if(count == 5)
            {
                if(!strcmp(buf , "12345"))
                {
                    //密码正确，正常退出
                    return 1;
                }
                else
                {
                    //密码错误，继续解锁
                    ShowJpg(map,"Game_Pic/password_false.jpg",0,0,1,1);
                    sleep(1);
                    count = 0;
                    break;
                }
            }
        }
    }
    
    return 0;
}